package com.settlement.system.service.impl;

import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.common.util.SecurityUtils;
import com.settlement.system.common.util.common.SystemUserUtil;
import com.settlement.system.mapper.BaseMapper;
import com.settlement.system.mapper.TblNcVurcherDetailMapper;
import com.settlement.system.mapper.security.userdetails.SysUserDetails;
import com.settlement.system.model.entity.common.SystemUser;
import com.settlement.system.model.entity.common.TblNcVurcherDetail;
import com.settlement.system.model.entity.common.TblReceiveRefundEditor;
import com.settlement.system.service.ITblNcVurcherDetailService;
import com.settlement.system.service.ITblReceiveRefundEditorService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * NC凭证明细 服务实现类
 *
 * @author Ximi
 * @since 2020-07-10
 */
@Service
@Slf4j
public class TblNcVurcherDetailServiceImpl extends BaseServiceImpl implements ITblNcVurcherDetailService {

    @Resource
    private TblNcVurcherDetailMapper tblNcVurcherDetailMapper;

    @Resource
    private ITblReceiveRefundEditorService tblReceiveRefundEditorService;

    /*@Reference(version = "${dubbo.acs.api.version}", timeout = 6000)
    private RrPushNcApi rrPushNcApi;*/

    @Override
    public BaseMapper init() {
        return tblNcVurcherDetailMapper;
    }

    @Override
    public TblNcVurcherDetail getFoot(Map<String, Object> params) throws ServiceException {
        try {
            return tblNcVurcherDetailMapper.getFoot(params);
        } catch (Exception ex) {
            log.error("获取合计异常", ex);
            throw new ServiceException("获取合计异常", ex);
        }
    }

    @Override
    public String revokeNc(Map<String, Object> params) throws ServiceException {
        try {
            // 先找出本账户对应的editNo
            SysUserDetails user = SecurityUtils.getUser();
            String username = user.getUsername();
            TblReceiveRefundEditor editor = tblReceiveRefundEditorService.getEditorByLoginName(username);

            // 当前用户不是制单人身份，提示"撤销人非制单人，故无法撤销！"
            if (editor == null) {
                return "撤销人非制单人，故无法撤销！";
            }
            String editorNo = editor.getEditorName();

            // 再看 editNo 不符合本editNo的NC凭证条数，如果大于0，那么返回
            params.put("notEditorName", editorNo);
            if (tblNcVurcherDetailMapper.selectCount(params) > 0) {
                return "选中了制单人不是当前用户的数据,不能执行撤销操作";
            }
            // 去掉参数
            params.remove("notEditorName");
            //rrPushNcApi.revokeNc(params);
        } catch (Exception ex) {
            log.error("撤销NC异常", ex);
            throw new ServiceException("撤销NC异常", ex);
        }
        return null;
    }
}

